package com.zr.stock.stockIdentification.service.impl;

import com.zr.stock.model.Stockidentification;
import com.zr.stock.stockIdentification.mapper.IdentificationMapper;
import com.zr.stock.stockIdentification.modelVo.IdentificationAddVo;
import com.zr.stock.stockIdentification.modelVo.IdentificationQueryVo;
import com.zr.stock.stockIdentification.modelVo.IdentificationUpdateVo;
import com.zr.stock.stockIdentification.modelVo.UpdateStatusVO;
import com.zr.stock.stockIdentification.service.IdentificationService;
import com.zr.stock.utils.AllRecords;
import com.zr.stock.utils.ResultVO;
import com.zr.stock.utils.ResultVOBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class IdentificationServiceImpl implements IdentificationService {
    @Autowired
    private IdentificationMapper identificationMapper;
    @Override
    public ResultVO<AllRecords<Stockidentification>> queryPage(IdentificationQueryVo identificationQueryVo) {
        AllRecords<Stockidentification> allRecords = new AllRecords<>();
        List<Stockidentification> data = identificationMapper.queryPage(identificationQueryVo);
        int count = identificationMapper.queryCount(identificationQueryVo);
        allRecords.setDataList(data);
        allRecords.setPageIndex(identificationQueryVo.getPageIndex());
        allRecords.setPageSize(identificationQueryVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultVOBuilder.success(allRecords);
    }

    @Override
    public ResultVO<IdentificationAddVo> warehouseTagAdd(IdentificationAddVo identificationAddVo) {
        //验证编码，名称不能重复
        IdentificationAddVo stockidentification1=identificationMapper.queryByCode(identificationAddVo.getCode());
        if(stockidentification1!=null){
            return ResultVOBuilder.error("500","编码已存在");
        }
        IdentificationAddVo stockidentification2=identificationMapper.queryByName(identificationAddVo.getName());
        if(stockidentification2!=null){
            return ResultVOBuilder.error("500","名称已存在");
        }
        identificationMapper.add(identificationAddVo);
        return ResultVOBuilder.success(identificationAddVo);
    }

    @Override
    public ResultVO<UpdateStatusVO> updateStatus(UpdateStatusVO updateStatusVO) {
        identificationMapper.updateStatus(updateStatusVO);
        return ResultVOBuilder.success(updateStatusVO);
    }

    @Override
    public ResultVO<IdentificationUpdateVo> warehouseTagUpdate(IdentificationUpdateVo identificationUpdateVo) {
        //乐观锁
        IdentificationAddVo stockidentification=identificationMapper.queryByCode(identificationUpdateVo.getCode());
        if(stockidentification.getVersion()!=identificationUpdateVo.getVersion()){
            return ResultVOBuilder.error("500","信息已被修改，刷新后重试");
        }
        IdentificationAddVo stockidentification2=identificationMapper.queryByName(identificationUpdateVo.getName());
        if(stockidentification2!=null){
            if(!stockidentification2.getCode().equals(identificationUpdateVo.getCode())){
                return ResultVOBuilder.error("500","名称已存在");
            }
        }
        identificationMapper.update(identificationUpdateVo);
        return ResultVOBuilder.success(identificationUpdateVo);
    }
}
